package com.zsc.edu.testrecyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    List<MyItemNet> mMyItemNetList = new ArrayList<>();

    RecyclerViewAdapter recyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化数据
        initMyItemList();

        RecyclerView recyclerView = findViewById(R.id.recycler);

        // 设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        // 设置布局的方向 默认垂直 ，设置成水平
        //linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        // 瀑布流 布局管理器
//        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(
//                2,  // 一行2列
//                StaggeredGridLayoutManager.VERTICAL // 方向垂直
//        );
//        recyclerView.setLayoutManager(staggeredGridLayoutManager);

        // 初始化适配器
        recyclerViewAdapter = new RecyclerViewAdapter(mMyItemNetList);

        recyclerViewAdapter.setContext(MainActivity.this);

        // 设置适配器
        recyclerView.setAdapter(recyclerViewAdapter);


    }


    private void initMyItemList() {

        // 初始化 子线程
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // url
                    //URL url = new URL("https://www.zsc.edu.cn");
                    //URL url = new URL("http://yeyunxiaopan.xyz/api/hotnews.json");
                    URL url = new URL("http://172.16.56.250:8000/api/hotnews");
                    // url 连接
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    // 请求方法，如果上传可用 POST
                    connection.setRequestMethod("GET");
                    // 连接超时
                    connection.setConnectTimeout(3000);
                    // 读取超时
                    connection.setReadTimeout(3000);
                    // 获得输入流
                    InputStream inputStream = connection.getInputStream();
                    // 输入流转成BufferedReader
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

                    String line;
                    StringBuilder response = new StringBuilder();
                    // 一次读取一行
                    while ((line = reader.readLine()) != null) {
                        // 每一行连接起来
                        response.append(line);
                    }

                    Log.d("---MainActivity---", "ResponseCode: " + connection.getResponseCode());
                    Log.d("---MainActivity---", "ResponseMessage: " + connection.getResponseMessage());
                    Log.d("---MainActivity---", "response: " + response);

                    // 请求结果，字符串转成JSON对象
                    JSONObject jsonObject = new JSONObject(response.toString());
                    // 根据key，获得 value，是一个JSON数组
                    JSONArray hotnews = jsonObject.getJSONArray("hotnews");
                    // 遍历数组
                    for (int i = 0; i < hotnews.length(); ++i) {
                        // 取出每一条新闻，也是一个JSON对象
                        JSONObject hotnew = hotnews.getJSONObject(i);

                        // 根据key，按照不同类型，获得 value
                        String news = hotnew.getString("news");
                        int hot = hotnew.getInt("hot");
                        int id = hotnew.getInt("id");
                        int share = hotnew.getInt("share");
                        String img = hotnew.getString("img");

                        MyItemNet myItemNet = new MyItemNet(
                                news,
                                img,
                                R.drawable.hot,
                                R.drawable.share,
                                hot,
                                share);
                        // 把每个单项数据，添加list里
                        mMyItemNetList.add(myItemNet);
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            recyclerViewAdapter.notifyDataSetChanged();
                        }
                    });


                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        // 启动子线程
        thread.start();

    }

}
